﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace Zxb.DayWork.Core
{
    public class CacheAssistant
    {
        public static List<string> Keys = new List<string>();
        private static CacheManager cacheManager = CacheFactory.GetCacheManager();

        public static List<object> Values
        {
            get
            {
                List<object> list = new List<object>();
                foreach (string key in Keys)
                {
                    object v = GetData(key);
                    if (v != null)
                    {
                        list.Add(v);
                    }
                }
                return list;
            }
        }

        public static void Add(string key, object value, FileDependency fileDep)
        {
            cacheManager.Add(key, value, CacheItemPriority.Normal, null, fileDep);
            Keys.Add(key);
        }

        public static void Remove(string key)
        {
            cacheManager.Remove(key);
            Keys.Remove(key);
        }

        public static object GetData(string key)
        {
            object result = cacheManager.GetData(key);
            if (result == null)
            {
                try
                {
                    Keys.Remove(key);
                }
                catch
                {

                }
            }
            return result;
        }
    }
}
